//
//  HJRepairTurnCarController.m
//  GAMCAfterSales
//
//  Created by 贺佳 on 2018/5/15.
//  Copyright © 2018年 glendale. All rights reserved.
//

#import "HJRepairTurnCarController.h"

#import "HJRepairTurnCarHeaderView.h"
#import "HJRepairTurnCarCell.h"

#import "HJRepairTurnCarModel.h"
#import "AppDelegate.h"


@interface HJRepairTurnCarController ()<UITableViewDelegate,UITableViewDataSource,HJRepairTurnCarHeaderViewDelegate,HJRepairTurnCarCellDelegate>
/**
 table
 */
@property(nonatomic,strong)UITableView*tableView;
/**
 table头部
 */
@property(nonatomic,weak)UIView *colorView;
/**
 数据源
 */
@property(nonatomic,strong)NSMutableArray*dataAry;
/**
 工单号
 */
@property(nonatomic,copy)NSString*orderNumber;
/**
 车牌号
 */
@property(nonatomic,copy)NSString*carNumber;
/**
 服务顾问
 */
@property(nonatomic,copy)NSString*adviser;
/**
 是否结清
 */
@property(nonatomic,copy)NSString*settlement;
/**
 页码
 */
@property(nonatomic,assign)NSInteger page;

@end

@implementation HJRepairTurnCarController

-(NSString *)adviser
{
    if (!_adviser) {
       NSUserDefaults *user=[NSUserDefaults standardUserDefaults];
        _adviser=[user objectForKey:@"EMPLOYEE_NO"];
    }
    return _adviser;
}
-(NSMutableArray *)dataAry
{
    if (!_dataAry) {
        _dataAry=[NSMutableArray array];
    }
    return _dataAry;
}
-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView=[[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate=self;
        _tableView.dataSource=self;
        _tableView.tableFooterView=[UIView new];
        _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        _tableView.frame=CGRectMake(self.colorView.mj_x, CGRectGetMaxY(self.colorView.frame), self.colorView.mj_w, self.view.mj_h-CGRectGetMaxY(self.colorView.frame));
        [self.view addSubview:_tableView];
        WEAKSELF;
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            
            weakSelf.page = 1;
            [weakSelf getData];
        }];
        _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            [weakSelf getData];
        }];
    }
    return _tableView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUI];

    [self getData];
}
-(void)setUI{
    self.view.backgroundColor=[UIColor whiteColor];
    //返回按钮
    UIButton*homeBackButton = [UIButton buttonWithType:UIButtonTypeCustom];
    UIImage*img=[UIImage imageNamed:@"房子.png"];
    [homeBackButton setImage:img forState:UIControlStateNormal];
    CGFloat h=40;
    CGFloat w=h/img.size.height*img.size.width;
    [homeBackButton setFrame:CGRectMake(60, 42, w, h)];
    [homeBackButton addTarget:self action:@selector(InformatBack) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:homeBackButton];
    
    //视图标题
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(200, 45, 200, 40)];
    titleLabel.text = @"维修交车";
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.textColor = [UIColor grayColor];
    titleLabel.font = [UIFont systemFontOfSize:30];
    [self.view addSubview:titleLabel];
    
    UIImageView *lineView = [[UIImageView alloc]initWithFrame:CGRectMake(20, 100, 1000, 11)];
    lineView.image = [UIImage imageNamed:@"渐变分割线.png"];
    [self.view addSubview:lineView];
    
    
    //查询按钮
    CGFloat btnW=58;
    CGFloat btnH=30;
    UIButton *vehiclequery = [[UIButton alloc]init];
    [vehiclequery setTitle:@"查 询" forState:UIControlStateNormal];
    [vehiclequery setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [vehiclequery setBackgroundImage:[UIImage imageNamed:@"ht05-1.png"] forState:UIControlStateNormal];
    [vehiclequery addTarget:self action:@selector(VehicleQuery) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:vehiclequery];
    [vehiclequery mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(btnW, btnH));
        make.right.mas_equalTo(lineView).inset(100);
        make.centerY.mas_equalTo(titleLabel);
    }];
    //头部条件视图
    HJRepairTurnCarHeaderView*conditionView=[[HJRepairTurnCarHeaderView alloc]initWithFrame:CGRectMake(homeBackButton.frame.origin.x, CGRectGetMaxY(lineView.frame), CGRectGetMaxX(lineView.frame)-2*homeBackButton.frame.origin.x, 120)];
    conditionView.delegate=self;
    [self.view addSubview:conditionView];
    
    //table头部
    NSMutableArray *labelMutabelArray = [[NSMutableArray alloc]initWithObjects:@"序号",@"工单号",@"维修类型",@"车牌号",@"服务顾问",@"送修人姓名",@"交车", nil];
    UIView *colorView = [[UIView alloc]initWithFrame:CGRectMake(conditionView.mj_x,CGRectGetMaxY(conditionView.frame)+40, conditionView.mj_w, 45)];
    self.colorView=colorView;
    colorView.backgroundColor = [[UIColor alloc]initWithRed:0.96 green:0.96 blue:0.96 alpha:1];
    [self.view addSubview:colorView];
    CGFloat labelW=colorView.mj_w/labelMutabelArray.count;
    for(int i = 0; i <labelMutabelArray.count;i++)
    {
        UILabel *titlelabel = [[UILabel alloc]initWithFrame:CGRectMake(i*labelW, 0, labelW, colorView.mj_h)];
        titlelabel.backgroundColor = [UIColor clearColor];
        titlelabel.text = [labelMutabelArray objectAtIndex:i];
        titlelabel.textColor = [UIColor grayColor];
        titlelabel.font = [UIFont fontWithName:@"ArialMT" size:16];
        [titlelabel setTextAlignment:NSTextAlignmentCenter];
        [colorView addSubview:titlelabel];
    }
    
}
-(void)getData{
    [SVProgressHUD showWithStatus:@"请等待...."];
    NSUserDefaults *user=[NSUserDefaults standardUserDefaults];
    NSString*carNumber=self.carNumber?self.carNumber:@"";
    carNumber =[carNumber stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSDictionary*dict=@{
                        @"ENTITY_CODE":[user objectForKey:@"ENTITY_CODE"],
                        @"RO_NO":self.orderNumber.length?self.orderNumber:@"",
                        @"LICENSE":carNumber,
                        @"SERVICE_ADVISOR":self.adviser.length?self.adviser:@"",
                        @"PAY_OFF":self.settlement.length?self.settlement:@"",
                        @"FROM":@"1",
                        @"TO":@(self.page*10)
                        };
    WEAKSELF;
    [MyAFNetWorkManager postWithParam:dict andAction:@"QUERY_BALANCEDRO_FOR_PAD" finish:^(NSData *data, NSDictionary *obj, NSError *error)
     {
         [SVProgressHUD dismiss];
         [weakSelf.tableView.mj_header endRefreshing];
         [weakSelf.tableView.mj_footer endRefreshing];

         if (error == nil)
         {
             [weakSelf.dataAry removeAllObjects];
             NSDictionary  *returnDic=(NSDictionary *)obj;
             NSLog(@"维修交车查询json is %@",obj);
             if ([[returnDic valueForKey:@"SUCCESS"]integerValue]==1)
             {
                 NSArray *array =[HJRepairTurnCarModel mj_objectArrayWithKeyValuesArray:returnDic[@"TT_REPAIR_ORDER"]];
                 if (array.count) {
                     weakSelf.page++;
                 }
                 weakSelf.dataAry=[array mutableCopy];
             }else{
                 [SVProgressHUD showWithStatus:returnDic[@"MSG"]];
             }
             
             [weakSelf.tableView reloadData];
         }
         else
         {
             NSLog(@"%@",error);
         }
     }];
}
-(void)VehicleQuery{
    self.page=1;
    [self getData];
}
-(void)InformatBack{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark --UITableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //
    return self.dataAry.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HJRepairTurnCarCell*cell=[HJRepairTurnCarCell cellWithTable:tableView];
    cell.delegate=self;
    cell.repairTurnCarModel=self.dataAry[indexPath.row];
    cell.indexPath=indexPath;
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row%2 == 1)
    {
        cell.backgroundColor = [[UIColor alloc]initWithRed:226.00/255 green:226.00/255 blue:226.00/255 alpha:1];
    }
    else
    {
        cell.backgroundColor = [UIColor whiteColor];
    }
}
#pragma mark --HJRepairTurnCarCellDelegate
-(void)repairTurnCarCell:(HJRepairTurnCarCell *)repairTurnCarCell submitCar:(HJRepairTurnCarModel *)repairTurnCarModel
{
    [SVProgressHUD showWithStatus:@"请等待...."];
    NSUserDefaults *user=[NSUserDefaults standardUserDefaults];
    NSDictionary*dict=@{
                        @"ENTITY_CODE":[user objectForKey:@"ENTITY_CODE"],
                        @"RO_NO":repairTurnCarModel.RO_NO
                        };
    WEAKSELF;
    [MyAFNetWorkManager postWithParam:dict andAction:@"GET_VEHICLE_For_Pad" finish:^(NSData *data, NSDictionary *obj, NSError *error)
     {
         [SVProgressHUD dismiss];
         
         if (error == nil)
         {
             NSDictionary  *returnDic=(NSDictionary *)obj;
             NSLog(@"交车提交json is %@",obj);
             if ([[returnDic valueForKey:@"SUCCESS"]integerValue]==1)
             {
                 ALERT(@"交车成功");
                 [weakSelf getData];
             }else{
                 ALERT(@"交车失败,请稍后再试");
             }
         }
         else
         {
             ALERT(@"交车失败,请稍后再试");
         }
     }];
}

#pragma mark --HJRepairTurnCarHeaderViewDelegate
-(void)repairTurnCarHeaderView:(HJRepairTurnCarHeaderView *)repairTurnCarHeaderView changeServiceAdviser:(NSString *)Adviser
{
    HJLog(@"%@",self.adviser);
    self.adviser=Adviser;
    HJLog(@"%@",self.adviser);
}
-(void)repairTurnCarHeaderView:(HJRepairTurnCarHeaderView *)repairTurnCarHeaderView changeIsSettlement:(NSString *)Settlement
{
    self.settlement=Settlement;
    HJLog(@"%@",self.settlement);
}
-(void)repairTurnCarHeaderView:(HJRepairTurnCarHeaderView *)repairTurnCarHeaderView changeOrderNumber:(NSString *)orderNumber
{
    self.orderNumber=orderNumber;
    HJLog(@"%@",self.orderNumber);
}
-(void)repairTurnCarHeaderView:(HJRepairTurnCarHeaderView *)repairTurnCarHeaderView changeCarNumber:(NSString *)carNumber
{
    self.carNumber=carNumber;
      HJLog(@"%@",self.carNumber);
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
